package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.hkyl.hospital.entity.*;
import com.hkyl.hospital.mapper.*;
import com.hkyl.hospital.mapper.custom.CustomHospitalApplyMapper;
import com.hkyl.hospital.service.HospitalApplysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@Service
@Transactional
public class HospitalApplysServiceImpl implements HospitalApplysService {

    @Autowired
    private CustomHospitalApplyMapper hospitalApplyMapper;

    @Autowired
    private HospitalizedMapper hospitalizedMapper;
    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private SickroomMapper sickroomMapper;

    @Override
    public List<HospitalApply> select(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<HospitalApply> hospitalApplies = hospitalApplyMapper.selectByExample(null);

        List<Patient> patients = patientMapper.selectByExample(null);

        for (Patient patient : patients) {
            hospitalApplies.stream().filter(hospitalApply -> hospitalApply.getPatientId() == patient.getPatientId()).forEach(hospitalApply -> hospitalApply.setPatient(patient));
        }

        List<Dept> depts = deptMapper.selectByExample(null);
        for (Dept dept : depts) {
            hospitalApplies.stream().filter(hospitalApply -> hospitalApply.getDeptId() == dept.getDeptId()).forEach(hospitalApply -> hospitalApply.setDept(dept));
        }

        List<User> users = userMapper.selectByExample(null);
        for(User user : users){
            hospitalApplies.stream().filter(hospitalApply -> hospitalApply.getDoctor() == user.getUserId()).forEach(hospitalApply -> hospitalApply.setUser(user));
        }

        List<Sickroom> sickrooms = sickroomMapper.selectByExample(null);
        hospitalApplies.stream().forEach(hospitalApply -> hospitalApply.setSickrooms(sickrooms));

        return hospitalApplies;
    }

    @Override
    public int changeStatus(Integer applyId) {
        HospitalApply hospitalApply = hospitalApplyMapper.selectByPrimaryKey(applyId);
        Integer status = hospitalApply.getStatus();
        int rows = 0;
        if(status == 0){
            rows = hospitalApplyMapper.changeIn(applyId);

        }else {
            rows = hospitalApplyMapper.changeOut(applyId);
        }
        return rows;
    }

    @Override
    public HospitalApply applyHospital(HospitalApply hospitalApply) {
        int rows = hospitalApplyMapper.insertSelective(hospitalApply);
        if (!(rows > 0)) {
            throw new RuntimeException("添加失败");
        }
        return hospitalApply;
    }

    @Override
    public HospitalApply updateApply(HospitalApply hospitalApply) {
        HospitalApply hospitalApply1 = hospitalApplyMapper.selectByPrimaryKey(hospitalApply.getApplyId());
        if (hospitalApply1.getStatus()==1) {
            throw new RuntimeException("您的申请已同意，请联系负责护士，进行调整");
        }
        int rows = hospitalApplyMapper.updateByPrimaryKeySelective(hospitalApply);
        if (!(rows > 0)) {
            throw new RuntimeException("编辑失败");
        }
        return hospitalApply;
    }
}
